﻿#ifndef _FILE_MANAGE_H_
#define _FILE_MANAGE_H_

#include "crossplat_def.h"
#include <string>
#include "cross_com.h"
#include "filename_create.h"
#include "rec_write_base.h"
#include <map>
#include "serial_file_base.h"

using namespace std;
typedef map<int,REC_FILE_LIST>  MAP_CH_FILE_T;

class CFile_manager
{
public:
	CFile_manager();
	~CFile_manager();

	int Init(char * storageName,int chNum,RECORD_DIR_INFO_S recDir);
	static int CreateRecordInfo(const char* storage_path,uint32_t recblk_sz,uint32_t indexInfoSize, int indexNum);
	//磁盘未满
	int GetNexRecordFile(int channel,string & fileName, uint32_t & fileIndex, uint32_t & fileSize,time_t recTm, int recType = F_NOR_REC);

	//循环录像回收录像文件
	int GetCycleIndex(uint32_t channel,string & filename, time_t recTm,unsigned short & diskIndex, uint32_t & indexNo);

	int GetNextPic(string & picName,time_t curTm, int channel);

	int RepairRecord();

	int CreateNewFile(string & fileName,time_t curTime,int fileType);

	int RecordStart(REC_FILE_INFO & recInfo);

	int RecordEnd(REC_FILE_INFO & recInfo);

	int SearchFile(int64_t tmStart, int64_t tmEnd,int recType, vector<uint8_t>& vecCH,
		uint32_t maxNum,MAP_CH_FILE_T& mapPerChFile,bool bForward = true);

	void AddToFileList(list<REC_FILE_INFO> & list, REC_FILE_INFO & index,uint32_t maxNum, uint32_t orderType);
	//停止录像
	
	uint32_t GetCurRecordSN();
	uint32_t GetNextRecordSN();
	uint32_t CreateNextRecordSN();
	
	int FindRecordFile(char * szPath,const char * file_ext,vector<string> & vec_recFile);
	int AnalysisFileList(vector<string> & vec_recFile);
	int AnalysisFileInfo(string & strFile, RECFILE_INFO & recInfo);
	int RegisterRecWriter(int ch, CRecWriteBase * pRec);

	int CreateThumbnail(char * recName);
	int DelThumbnail(char * recName);

	void     GetMaxTime(time_t & maxTm);
	bool     IsDiskFull();
	uint32_t GetRecFreeSpaceMB();
	uint32_t GetDiskFreeSpaceMB();
	int  GetDiskInfo();
	int GetFilePath(char * fileName, FILE_INFO & fInfo);

	char * GetDiskName()
	{
		return m_dev;
	}
	
	void SetRecFileSize(uint32_t recSize)
	{
		m_recFileSize = recSize;
	}

	uint32_t GetRecFileSize()
	{
		return m_recFileSize;
	}

	uint32_t GetCycleFileSize()
	{
		return m_cycleFileSize;
	}

	char * get_device_name()
	{
		return m_filename;
	}
#ifdef _SIMULATE_RECORD_
	void SetStoragePath(char* path)
	{
		m_fCreater.SetMntDir(path);
	}
#endif
	void SetNorSpace(uint32_t percent)
	{
		m_norDir_percent = percent;
	}

	uint32_t GetNorSpace()
	{
		return m_norDir_percent;
	}
protected:
	char			   m_dev[20];
	char			   m_filename[40];		            //通用文件名
	CSerialFileBase  *  m_pFile;
	CCross_Lock	       m_lock;
	CFilename_Createer m_fCreater;
	uint32_t		   m_recordSN;                       //录像块序列号
	uint32_t		   m_leastSN;                       //最小的序列号
	map<int,CRecWriteBase*> m_mpRecording;				//每个通道 正在录像的文件
	uint32_t		   m_recFileSize;					//单个文件的大小
	uint32_t           m_norDir_percent;				//正常录像的配额
	uint32_t           m_total_space_MB;                //总共空间
	uint32_t           m_cycleFileSize;                 //回收文件大小
	map<uint32_t,RECFILE_INFO> m_map_fileInfo;          //录像文件位表，用于循环覆盖使用
	int                m_diskIndex;
};

typedef map<uint32_t, RECFILE_INFO> MAP_RECFILE_INFO;

#endif
